package com.example.demo.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.OperLog;
import com.example.demo.service.LogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @description：日志定时清理任务.
 * @author：lw
 * @create_time：2024/1/19
 **/

@Component
@Slf4j
public class LogClearTask {

    @Autowired
    LogService logService;


    @Scheduled(cron = "0 0 2/24 * * ?")       // https://cron.ciding.cc/在线生成工具
    public void LogClear() {   // 每天凌晨两点清理前一天的日志
        long current=System.currentTimeMillis();
        Date startDate =new Date(current - 1000*60*60*26);
        Date endDate =new Date(current - 1000*60*60*2);
        QueryWrapper<OperLog> queryWrap=new QueryWrapper<>();
        queryWrap.ge("create_time",startDate);
        queryWrap.le("create_time",endDate);
        logService.remove(queryWrap);
    }
}
